from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from goods.models import SPU, Brand, GoodsCategory
from meiduo_admin.serializers.goods import BrandSerializer, SPUSerializer
from meiduo_admin.serializers.skus import GoodsCategorySerializer
from meiduo_admin.utils import PageNum


class SPUView(ModelViewSet):
    # 指定序列化器
    serializer_class = SPUSerializer
    # 指定查询集
    queryset = SPU.objects.all()
    # 指定分页器
    pagination_class = PageNum
    # 指定权限
    permission_classes = [IsAdminUser]

    def simple(self, request):
        brands = Brand.objects.all()
        ser = BrandSerializer(brands, many=True)
        return Response(ser.data)


    def channel(self,request):
        # 1、获取一级分类数据
        data = GoodsCategory.objects.filter(parent=None)
        # 2、序列化返回分类数据
        ser=GoodsCategorySerializer(data,many=True)
        return Response(ser.data)

    def channels(self, request, pk):
        # 1、获取二级和三级分类数据
        data = GoodsCategory.objects.filter(parent_id=pk)
        # 2、序列化返回分类数据
        ser = GoodsCategorySerializer(data, many=True)
        return Response(ser.data)